package com.PVZ.controller;

import com.PVZ.utils.ThreadPool;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;

/**
 * Created by @Author: kabuda on @Date: 2021/5/19 20:31
 */

@Getter
@Setter
@NoArgsConstructor
public class SoundController extends Thread {

    public static final int LOOP = -1;
    public static final int ONCE = 0;

    private static Clip clip;
    /**
     * 播放bgm的Clip
     * 因为只有一个 所以设置为static
     */
    private static Clip bgmClip;

    /**
     * 播放声音
     *
     * @param musicName 声音文件名(xxx.wav)
     * @param type      -1 循环 0 不播放 1 播放一次
     */
    public static void playSound(String musicName, int type) {
        File file = new File("res/audio/" + musicName);

        ThreadPool.cachedThreadPool.execute(() -> {
            try {
                AudioInputStream audio;
                audio = AudioSystem.getAudioInputStream(file);
                clip = AudioSystem.getClip();
                clip.open(audio);
                clip.loop(type);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

}
